package com.dayu.gulimall.controller;

import com.dayu.common.exception.BizCodeEnum;
import com.dayu.common.to.es.SkuEsModel;
import com.dayu.common.utils.R;
import com.dayu.gulimall.service.ElaticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author dayu
 * @version 1.0
 * @email dayu007@foxmail.com
 * @date 2020/12/6 20:47
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    ElaticSaveService elaticSaveService;

    /**
     * 商品上架
     * @param esModelList
     * @return
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuEsModel> esModelList) {
        boolean b = false;
        try {
            b = elaticSaveService.productStatusUp(esModelList);
        } catch (IOException e) {
            log.error("商品上架错误：{}", e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (b) {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        } else {
            return R.ok();
        }
    }
}
